using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Betarium.PotonEditor
{
	public class ToolInfoRect : ToolInfo
	{
//		Rectangle m_Rect;
//		bool m_bMove;
		public ToolInfoRect(EditViewForm form)
			: base(form)
		{
		}

		public override void OnMouseDown(object sender, MouseEventArgs e)
		{
			m_Form.SelectState.OnMouseDown(sender, e);
			/*
			if (e.Button == MouseButtons.Left)
			{
				m_bMove = true;
			}
			*/
		}
		public override void OnMouseUp(object sender, MouseEventArgs e)
		{
			/*
			bool bMouseLeftDown = m_Form.Selection.MouseLeftDown;
			m_bMove &= bMouseLeftDown;
			m_Form.Selection.OnMouseUp(sender, e);
			if (m_bMove && bMouseLeftDown && e.Button == MouseButtons.Left)
			{
				if (m_Rect.Width * m_Rect.Height > 0)
				{
					Graphics graph = Graphics.FromImage(m_Form.Image);
					graph.FillRectangle(new SolidBrush(m_Form.ColorState.MainColor), m_Rect);
					graph.Dispose();
					m_Form.SetModified(true);
				}
			}
			m_bMove = false;
			m_Rect = Rectangle.Empty;
			*/
			bool bMouseLeftDown = m_Form.SelectState.MouseLeftDown;
			m_Form.SelectState.OnMouseUp(sender, e);
			if (bMouseLeftDown
				&& e.Button == MouseButtons.Left
				&& m_Form.SelectState.MouseMoved)
			{
                this.m_Form.AddUndo();
				using (Graphics graph = Graphics.FromImage(m_Form.Image))
				{
					OnDraw(graph);
				}
				SetModified();
			}
		}
		public override void OnMouseMove(object sender, MouseEventArgs e)
		{
			m_Form.SelectState.OnMouseMove(sender, e);
			if (m_Form.SelectState.MouseLeftDown)
			{
				m_Form.Cursor = Cursors.Cross;
			}
			else
			{
				m_Form.Cursor = Cursors.Arrow;
			}
			/*
			m_bMove &= m_Form.Selection.MouseLeftDown;
			Point mouseMoveBefore = m_Form.Selection.MouseBeforePos;
			Point cursorPos = m_Form.Selection.MouseCurrentPos;
			Point mouseMoveBegin = m_Form.Selection.MouseBeginPos;
			if (m_bMove && m_Form.Selection.MouseLeftDown)
			{
				Point selectSize = cursorPos;
				selectSize.Offset(-mouseMoveBegin.X, -mouseMoveBegin.Y);
				Point selectBegin = mouseMoveBegin;
				if (selectSize.X < 0)
				{
					selectSize.X = -selectSize.X;
					selectBegin.X = mouseMoveBegin.X - selectSize.X;
				}
				if (selectSize.Y < 0)
				{
					selectSize.Y = -selectSize.Y;
					selectBegin.Y = mouseMoveBegin.Y - selectSize.Y;
				}
				m_Rect = new Rectangle(selectBegin, new Size(selectSize));
				m_Form.Cursor = Cursors.Cross;
			}
			else
			{
				m_Form.Cursor = Cursors.Arrow;
			}
			*/
		}
		public override void OnPaint(object sender, PaintEventArgs e)
		{
			if (m_Form.SelectState.MouseLeftDown)
			{
				OnDraw(e.Graphics);
			}
		}
		public override void OnDraw(Graphics graph)
		{
			if (!m_Form.SelectState.MouseMoved)
				return;
			switch (ColorState.FillMode)
			{
				case PaintInfo.FillModes.Outer:
					graph.DrawRectangle(new Pen(m_Form.PaintState.MainColor), SelectState.MouseMoveRect);
					break;
				case PaintInfo.FillModes.Both:
					graph.FillRectangle(new SolidBrush(m_Form.PaintState.SubColor), SelectState.MouseMoveRect);
					graph.DrawRectangle(new Pen(m_Form.PaintState.MainColor), SelectState.MouseMoveRect);
					break;
				case PaintInfo.FillModes.Inner:
					graph.FillRectangle(new SolidBrush(m_Form.PaintState.MainColor), SelectState.MouseMoveRect);
					break;
			}
		}
	}
}
